
import 'package:flutter/material.dart';
import 'package:flutter_ideal/common/data.dart';
final widgetDirectories = [
  new ItemData("基础组件", "在构建您的第一个Flutter应用程序之前，您绝对需要了解这些widget。"),
  new ItemData(
      "Material Components", "实现了Material Design 指南的视觉、效果、motion-rich的widget。"),
  new ItemData("Cupertino(iOS风格的widget)", "用于当前iOS设计语言的美丽和高保真widget。"),
  new ItemData("Layout", "排列其它widget的columns、rows、grids和其它的layouts。"),
  new ItemData("Text", "文本显示和样式"),
  new ItemData("Assets、图片、Icons", "管理assets, 显示图片和Icon。"),
  new ItemData("Input", "Material Components 和 Cupertino中获取用户输入的widget。"),
  new ItemData("动画和Motion", "在您的应用中使用动画。查看Flutter中的动画总览"),
  new ItemData("交互模型", "响应触摸事件并将用户路由到不同的页面视图（View）。"),
  new ItemData("样式", "管理应用的主题，使应用能够响应式的适应屏幕尺寸或添加填充。"),
  new ItemData("绘制和效果", "Widget将视觉效果应用到其子组件，而不改变它们的布局、大小和位置。"),
  new ItemData("Async", "Flutter应用的异步模型"),
  new ItemData("滚动", "滚动一个拥有多个子组件的父组件"),
  new ItemData("辅助功能", "给你的App添加辅助功能(这是一个正在进行的工作)"),
];

class HomePage extends StatefulWidget{
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return new _HomePageState();
  }

}
class _HomePageState extends State<HomePage>{

  final GlobalKey<AnimatedListState> _listKey = new GlobalKey<AnimatedListState>();

  ListModel<ItemData> _list;

  @override
  void initState() {
    super.initState();
    _list = new ListModel<ItemData>(
      listKey: _listKey,
      initialItems: widgetDirectories,
      removedItemBuilder: _buildRemovedItem,
    );
  }
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return new Scaffold(body: new Padding(
      padding: const EdgeInsets.all(16.0),
      child: new AnimatedList(
        key: _listKey,
        initialItemCount: _list.length,
        itemBuilder: _buildItem,
      ),
    ),);
  }
  // Used to build list items that haven't been removed.
  Widget _buildItem(BuildContext context, int index, Animation<double> animation) {
    return new ConversationItem(
      animation: animation,
      item: _list[index],
//      selected: _selectedItem == _list[index],
      onTap: () {
        setState(() {
          //_selectedItem = _selectedItem == _list[index] ? null : _list[index];
        });
      },
    );
  }
  Widget _buildRemovedItem(ItemData item, BuildContext context, Animation<double> animation) {
    return new ConversationItem(
      animation: animation,
      item: item,
      selected: false,
      // No gesture detector here: we don't want removed items to be interactive.
    );
  }


}
class ListModel<E> {
  ListModel({
    @required this.listKey,
    @required this.removedItemBuilder,
    Iterable<E> initialItems,
  }) : assert(listKey != null),
        assert(removedItemBuilder != null),
        _items = new List<E>.from(initialItems ?? <E>[]);

  final GlobalKey<AnimatedListState> listKey;
  final dynamic removedItemBuilder;
  final List<E> _items;

  AnimatedListState get _animatedList => listKey.currentState;

  void insert(int index, E item) {
    _items.insert(index, item);
    _animatedList.insertItem(index);
  }

  E removeAt(int index) {
    final E removedItem = _items.removeAt(index);
    if (removedItem != null) {
      _animatedList.removeItem(index, (BuildContext context, Animation<double> animation) {
        return removedItemBuilder(removedItem, context, animation);
      });
    }
    return removedItem;
  }

  int get length => _items.length;
  E operator [](int index) => _items[index];
  int indexOf(E item) => _items.indexOf(item);
}
class ConversationItem extends StatelessWidget {
  const ConversationItem({
    Key key,
    @required this.animation,
    this.onTap,
    @required this.item,
    this.selected: false
  }) : assert(animation != null),
        assert(item != null),
        assert(selected != null),
        super(key: key);

  final Animation<double> animation;
  final VoidCallback onTap;
  final ItemData item;
  final bool selected;

  @override
  Widget build(BuildContext context) {
    TextStyle textStyle = Theme.of(context).textTheme.display1;
    if (selected)
      textStyle = textStyle.copyWith(color: Colors.lightGreenAccent[400]);
    return new Padding(
      padding: const EdgeInsets.all(2.0),
      child: new SizeTransition(
        axis: Axis.vertical,
        sizeFactor: animation,
        child: new GestureDetector(
          behavior: HitTestBehavior.opaque,
          onTap: onTap,
          child: new SizedBox(
            height: 88.0,
            child: new Card(
//              color: Colors.primaries[item % Colors.primaries.length],
              child: new Row(
//                  mainAxisAlignment: MainAxisAlignment.spaceEvenly,      //对齐方式：平均间隔
                  children:[
                Image.asset("images/ic_launcher_round.png"),
                new Column(
                    mainAxisAlignment: MainAxisAlignment.start,
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children:[
                  new Text(item.title,style: new TextStyle(color: Colors.black),
                  maxLines: 1,
                  ),
                new Text(item.description,style: new TextStyle(color: Colors.black38),
                maxLines: 1,)
                ]
                )
               ])
              ),
            ),
          ),
        ),
    );
  }
}
